package com.shse.sale.web.controller;

import com.shse.sale.meta.Content;
import com.shse.sale.meta.User;
import com.shse.sale.service.ContentService;
import com.shse.sale.service.TrxService;
import com.shse.sale.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 供api使用controller
 * Created by shse on 4/20/2016.
 */
@Controller
@RequestMapping("/api")
public class ApiController {

    /**
     * service注入
     */
    @Autowired
    private UserService userService;
    @Autowired
    private ContentService contentService;
    @Autowired
    private TrxService trxService;


    /**
     * 登陆api
     *
     * @param userName 用户名
     * @param password 密码
     * @param map Map
     * @param session HttpSession
     * @return 不存在的视图名，实现自动转换json
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        Map map,
                        HttpSession session) {

        //按照输入用户名密码查找用户
        User user = userService.login(userName, password);

        //如果用户存在
        if (user != null) {
            session.setAttribute("user", user);
            map.put("code", 200);
            map.put("message", "");
            map.put("result", true);
        } else {
            //如果用户不存在
            map.put("code", 401);
            map.put("message", "登陆失败");
            map.put("result", false);
        }

        return "json";
    }

    /**
     * 删除商品api
     *
     * @param id 商品id
     * @param session HttpSession
     * @param map ModelMap
     * @return 不存在的视图名，实现自动转换json
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestParam("id") String id,
                         HttpSession session,
                         ModelMap map) {
        Content content = contentService.getContentById(id);
        User user = (User) session.getAttribute("user");
        if (user != null && user.getUsertype() == 1 && content != null) {
            contentService.deleteContent(id);
            map.put("code", 200);
            map.put("messgae", "删除成功");
            map.put("result", true);
        } else {
            map.put("code", 404);
            map.put("message", "删除失败");
            map.put("result", false);
        }
        return "json";
    }

    /**
     * 购买商品api
     *
     * @param id 商品id
     * @param map ModelMap
     * @param session HttpSession
     * @return 不存在的视图名，实现自动转换json
     */
    @RequestMapping(value = "/buy", method = RequestMethod.POST)
    public String buy(@RequestParam("id") String id,
                      ModelMap map,
                      HttpSession session) {
        Content content = contentService.getContentById(id);
        User user = (User) session.getAttribute("user");
        if (user != null && user.getUsertype() == 0 && content != null) {
            trxService.addTrx(content, user.getId());
            map.put("code", 200);
            map.put("message", "购买成功");
            map.put("result", true);
        } else {
            map.put("code", 404);
            map.put("message", "购买失败");
            map.put("result", false);
        }
        return "json";
    }


}
